<?php

class People extends MY_SecuredController {

	function People() {
		parent::MY_SecuredController();

		$this->requireRole('Admin');

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->library('formdate');
	}

	function index() {
		$this->_preRender(Current_User::user()->id);

		$data['people'] = Doctrine::getTable('PersonModel')->findAll();

		$this->render('admin/people/index', $data);
	}

	function see($id) {
		$this->_preRender(Current_User::user()->id);

		$data['person'] = PersonModel::getPerson($id);
		$data['selected'] = $id;

		if (!$data['person']) {
			$this->redirect('admin/people/index', array(
                    'message' => 'Ooops, that person could not be found - choose another one.',
			), true);
		}

		//$data['people'] = Doctrine::getTable('PersonModel')->findAll();

		$this->render('admin/people/view', $data);
	}


	function ajax($op, $personid) {
		if ($op == 'view') {
			$person = PersonModel::getPerson($personid)->toArray();

			echo json_encode($person);
		}
	}

	public function ajax_acquery($query) {
		$this->_preRender(Current_User::user()->id);
		
		if (!isset($query)) {
			return;
		}
		
		$q = new Doctrine_RawSql();
		$result = $q->select('{p.name}')
		->from('portal_person p')
		->addComponent('p', 'PersonModel')
		->where('locate(\''.$query.'\',p.name) > 0')
		->orderBy('locate(\''.$query.'\',p.name)')
		->limit(10);

		log_message('debug', ''.$q->getSqlQuery());

		$result = $q->execute()->toArray();

		if ($result) {
			echo '<ul>'."\n";
			foreach ($result as $l) {
				$p = $l['name'];
				$p = preg_replace('/(' . $query . ')/i', '<span style="font-weight:bold;">$1</span>', $p);
				echo "\t".'<li id="autocomplete_'.$l['id'].'" rel="'.$l['id'].'_' . $l['id'] . '">'. ( $p ) .'</li>'."\n";
			}
			echo '</ul>';

		}
	}

	function add() {
		$this->_preRender(Current_User::user()->id);

		if (!$this->input->post('action')) {

			$data['person'] = new PersonModel();
			$this->render('admin/people/add', $data);
		}
		else {
			if ($this->_submit_validate() === FALSE) {
				$data['person'] = new PersonModel();
				$this->render('admin/people/add', $data, $this->error(validation_errors('<li>','<li>')));
				return;
			}

			$c = new PersonModel();
			$c->merge($_POST);

			try {
				$c->save();
			}
			catch (Exception $e) {
				$this->render('admin/people/index', $data, $this->error(array(
	                'message' => 'Ooops, the person could not be added - please try again later.',
	                'exception' => $e
				)));
				return;
			}

			$this->redirect('admin/people/index','Person added successfully!');
		}
	}

	function edit($id) {
		$this->_preRender(Current_User::user()->id);
		$data['people'] = Doctrine::getTable('PersonModel')->findAll();
			
		if (!$this->input->post('action')) {
			$this->_preRender(Current_User::user()->id);

			$data['person'] = PersonModel::getPerson($id);

			$this->render('admin/people/edit', $data);
		}
		else {
			$person = PersonModel::getPerson($id);

			if (!$person) {
				$this->render('admin/people/index',$data, $this->error(array(
                    'message' => 'Ooops, the person could not be found, operation has been cancelled!',
				)));
				return;
			}

			$person->merge($_POST);

			try {
				$person->save();
			}
			catch (Exception $e) {
				$this->render('admin/people/index', $data, $this->error(array(
                    'message' => 'Ooops, the information could not be updated, please try again later.',
                    'exception' => $e
				)));
				return;
			}

			$this->redirect('admin/people/index', 'Person has been successfully updated!');
		}
	}

	function delete($id) {
		$this->_preRender(Current_User::user()->id);
		$data['people'] = Doctrine::getTable('PersonModel')->findAll();

		$person = PersonModel::getPerson($id);

		if (!$person) {
			$this->redirect('admin/people/index', array(
                    'message' => 'Ooops, the person could not be found, operation has been cancelled!',
			));
			return;
		}

		try {
			$person->delete();
		}
		catch (Exception $e) {
			$this->redirect('admin/people/index', array(
                    'message' => 'Ooops, the person could not be deleted, please try again later.',
                    'exception' => $e
			), true);

			return;
		}

		$this->redirect('admin/people/index', 'Person has been successfully deleted!');
	}

	private function _submit_validate() {
		$this->form_validation->set_rules('name', 'Name',
			'required|alpha_numeric[Person.name]');

		return $this->form_validation->run();

	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */